<template>
  <div>
    <el-form ref="pram" :model="pram" :rules="rules" label-width="100px" @submit.native.prevent>
      <el-form-item label="政治面貌" prop="zzmm" class="dislabel">
        <el-select v-model="pram.zzmm" placeholder="政治面貌" clearable @change="onChangeShowTimePicker" style="width: 100%">
          <el-option
            v-for="(item,index) in zzmmList"
            :key="index"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="入党时间" prop="rdTime" v-if="showTimePicker" class="dislabel">
        <el-date-picker v-model="pram.rdTime" value-format="yyyy-MM-dd" format="yyyy-MM-dd" size="small" type="date" placement="bottom-end" style="width: 100%;" @change="onchangeTime" placeholder="入党时间"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handlerSubmit('pram')" v-hasPermi="['admin:person:update','admin:person:save']">{{ isCreate===0?'确定':'更新' }}</el-button>
        <el-button @click="close">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {Debounce} from '@/utils/validate'
export default {
  // name: "edit"
  components: { },
  props: {
    isCreate: {
      type: Number,
      required: true
    },
    editData: {
      type: Object,
      default: () => {
        return { rules: [] }
      }
    },
    allList: {
      type: Array
    }
  },
  data() {
    return {
      constants:this.$constants,
      zzmmList: [],
      zzmmKey:null,
      showTimePicker:false,

      pram: {
        zzmm: null,
        rdTime: null,
      },
      rules: {
        zzmm: [{ required: true, message: '请选择政治面貌', trigger: ['blur', 'change'] }],
        rdTime: [{ required: true, message: '请选择入党时间', trigger: ['blur', 'change'] }],
      },
    }
  },
  mounted() {
    this.initEditData()
    this.setShowStatus(this.pram.zzmm)
  },
  methods: {
    close() {
      this.$emit('hideRdTimeDialog')
    },
    setShowStatus(e){
      if(!e) return

      this.zzmmList.forEach((val, index) => {
        if(val.id === e) this.zzmmKey = index
      })
      const zzmmList = this.zzmmList
      if (zzmmList[this.zzmmKey].name === '中共党员') {
        this.showTimePicker = true
      }else{
        this.showTimePicker = false
        this.pram.rdTime = null
      }
    },
    onChangeShowTimePicker(e) {
      this.setShowStatus(e)
      this.pram.zzmm = e
    },
    onchangeTime (e) {
      this.pram.rdTime = e;
    },

    initEditData() {
      //if (this.isCreate !== 1) return //编辑
      this.zzmmList = this.allList
      const { zzmm, rdTime} = this.editData //通过list中slot-scope=“scope“，scope.$index就是该行的下标，scope.row就是该行的数据所有消息对象，有了这两个参数就可以实现编辑、删除功能（this.editData就是scope.row传入的对象参数）
      this.pram.zzmm = zzmm
      this.pram.rdTime = rdTime
    },
    handlerSubmit:Debounce(function(form) {
      this.$refs[form].validate(valid => {
        if (!valid) return
        if (this.isCreate === 0) {
          this.handlerSave()
        } else {
          this.handlerEdit()
        }
      })
    }),
    handlerSave() {
      this.$emit('assignRdTimeAndHideRdTimeDialog',this.pram)
    },
    handlerEdit() {
      this.$emit('assignRdTimeAndHideRdTimeDialog',this.pram)
    },
  }
}
</script>

<style scoped lang="scss">
  .dislabel{
    ::v-deep.el-input--small .el-input__inner,
    ::v-deep.el-input--medium .el-input__inner{
      height:36px;
      line-height:36px;
      font-size:12px;
    }

  }
</style>
